package com.atguigu.day05;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.util.Collector;

// Watermark(-MAX)
// Watermark(-5000L)
// (hello, 1000)
// Watermark(MAX)
public class Example8 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new SourceFunction<String>() {
                    @Override
                    public void run(SourceContext<String> ctx) throws Exception {
                        ctx.emitWatermark(new Watermark(-5000L));
                        // 第一个参数：要发送的数据
                        // 第二个参数：数据的事件时间
                        ctx.collectWithTimestamp("hello", 1000L);
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .process(new ProcessFunction<String, String>() {
                    @Override
                    public void processElement(String in, Context ctx, Collector<String> out) throws Exception {
                        out.collect("输入数据：" + in + ",当前process并行子任务的水位线：" +
                                "" + ctx.timerService().currentWatermark());
                    }
                })
                .print();

        env.execute();
    }
}
